﻿/************************************************************************

 This file is part of "10 Second Tim".

 "10 Second Tim" is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 "10 Second Tim" is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with "10 Second Tim".  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2013 Scott Bevin. all rights reserved

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LDE;
using LDE.Input;
using LudumDare27.Games;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace LudumDare27.GameScreens
{
	internal class ChooseInputScreen : GameScreen
	{
		private Texture2D logoTexture;
		private Menu menu;

		private WhiteFadeScreen whiteFade = null;

		private SoundEffect sfx;

		private InputTrigger m_inputTrigger = new InputTrigger( "PressStart" ).Add( Buttons.Start ).Add( Keys.Space ).Add( Buttons.A ).Add( MouseInputs.LeftButton )
			.Add( MouseInputs.RightButton );

		public ChooseInputScreen( )
			: base( true )
		{
		}

		public override void OnAdded( )
		{
			sfx = ContentManager.Load<SoundEffect>( "Sfx/MenuSelect" );

			Player.Get.GetInput( this ).ClearDevices( );

			logoTexture = ContentManager.Load<Texture2D>( "Img/GameLogo" );
			menu = new Menu( ContentManager.Load<SpriteFont>( "Fonts/DebugFont" ), ContentManager.Load<SpriteFont>( "Fonts/DebugFont" ), ContentManager );

#if !SILVERLIGHT
			menu.AddNonClickEntry( "Click Or Press Start / Spacebar", null );
#else
			menu.AddNonClickEntry( "Click To Play", null );
#endif
			menu.AddNonClickEntry( " ", null );
			menu.AddNonClickEntry( "www.gameXcore.co.uk", null );
			menu.AddNonClickEntry( "scott@gameXcore.co.uk", null );
			menu.AddNonClickEntry( "Ludum Dare 27 - http://www.ludumdare.com/", null );

			base.OnAdded( );
		}

		public override void OnRemoved( )
		{
			base.OnRemoved( );
		}

		public override void Update( float dt )
		{
			menu.Update( dt, Player.Get.GetInput( this ), this.Game.GraphicsDevice );

			if ( whiteFade == null )
			{
				IInputDevice iDevice = Game.InputManager.FindDeviceTappingInput( m_inputTrigger );
				InputHandler inputHndl = Player.Get.GetInput( this );

				if ( iDevice != null && inputHndl != null )
				{
					// if this is a gamepad then add it
#if SILVERLIGHT || XBOX || WINDOWS
					if ( iDevice.GetDeviceTypeId( ) == GamePadInputDevice.DeviceTypeId )
					{
						inputHndl.AddDevice( iDevice );
						Game.IsMouseVisible = false;
					}

					// if its a keyboard of mouse then try to add the other as well
					else
#endif
					{
#if WINDOWS || SILVERLIGHT
						if ( iDevice.GetDeviceTypeId( ) == KeyboardInputDevice.DeviceTypeId )
						{
							inputHndl.AddDevice( iDevice );
							inputHndl.AddDevice( Game.InputManager.FindDevice( MouseInputDevice.DeviceTypeId, 0 ) );

							Game.IsMouseVisible = true;
						}
						else if ( iDevice.GetDeviceTypeId( ) == MouseInputDevice.DeviceTypeId )
						{
							inputHndl.AddDevice( iDevice );
							inputHndl.AddDevice( Game.InputManager.FindDevice( KeyboardInputDevice.DeviceTypeId, 0 ) );
							Game.IsMouseVisible = true;
						}
#endif
					}

					sfx.Play( );

					whiteFade = new WhiteFadeScreen( );
					GameScreenManager.AddGameScreen( whiteFade, null, LDE.GameScreenManager.AddGameScreenData.PositioningOptions.OnTop );
				}
			}

			if ( whiteFade != null && whiteFade.State == WhiteFadeScreen.States.FadedIn )
			{
				whiteFade.FadeOut( );

				//GameScreenManager.AddGameScreen( ( (LD27Game)Game ).ChooseNextGame( null, 1 ), whiteFade, LDE.GameScreenManager.AddGameScreenData.PositioningOptions.BelowRef );

#if !SILVERLIGHT
				GameScreenManager.AddGameScreen( new MainMenuScreen( ), whiteFade, LDE.GameScreenManager.AddGameScreenData.PositioningOptions.BelowRef );
#else
				 				GameScreenManager.AddGameScreen( new GameplayIntroScreen( false, 0 ), whiteFade, LDE.GameScreenManager.AddGameScreenData.PositioningOptions.BelowRef );
#endif

				//GameScreenManager.AddGameScreen( new GameplayIntroScreen( false, 0 ), whiteFade, LDE.GameScreenManager.AddGameScreenData.PositioningOptions.BelowRef );
				this.KillScreen = true;
			}

			base.Update( dt );
		}

		public override void Render( float dt, GraphicsDevice gd, SpriteBatch sprites )
		{
			Vector2 basePosition = new Vector2( ( (float)gd.PresentationParameters.BackBufferWidth ) * 0.5f,
				( (float)gd.PresentationParameters.BackBufferHeight ) * 0.25f );

			sprites.Begin( SpriteSortMode.Deferred, BlendState.NonPremultiplied );

			sprites.Draw( logoTexture, basePosition, null, Color.White, 0.0f,
				new Vector2( ( (float)logoTexture.Width ) * 0.5f, ( (float)logoTexture.Height ) * 0.5f ), Vector2.One, SpriteEffects.None, 0.0f );

			sprites.End( );

			menu.Render( gd, sprites );

			base.Render( dt, gd, sprites );
		}
	}
}